﻿<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="field_name" lay-verify="required" required>
                            <option value="account">账号</option>
                            <option value="nickname">昵称</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="field_value" class="layui-input" placeholder="输入搜索内容"/>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="role_id">
                                <option value="">选择角色</option>
                                {{volist name="role_list" id="item"}}
                                <option value="{{$item.id}}">{{$item.role_name}}</option>
                                {{/volist}}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="search" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="test" lay-filter="test"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="resetPassword">重置密码</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="statusDemo">
    <input type="checkbox" lay-filter="editStatus" value="{{d.id}}" lay-skin="switch"
           lay-text="可用|禁用" {{d.status==0?'':'checked'}} style="display: none;"/>
    <p style="display: none;">{{d.status==0?'禁用':'可用'}}</p>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="adminSaveDialog">
    <form id="adminSaveForm" lay-filter="adminSaveForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">账号</label>
            <div class="layui-input-block">
                <input name="account" id="account" placeholder="账号由(a-zA-Z0-9._@)组成,6-30个字符" class="layui-input"
                       lay-verType="tips" lay-verify="required|account" autocomplete="off" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">昵称</label>
            <div class="layui-input-block">
                <input name="nickname" placeholder="昵称最长30个字符" class="layui-input"
                       lay-verType="tips" lay-verify="required|nickname" autocomplete="off" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色</label>
            <div class="layui-input-block">
                <select name="role_id" lay-verify="required" required>
                    <option value="">选择角色</option>
                    {{volist name="role_list" id="item"}}
                    <option value="{{$item.id}}">{{$item.role_name}}</option>
                    {{/volist}}
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="adminSaveSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<script type="text/html" id="resetPasswordDialog">
    <form id="resetPasswordForm" lay-filter="resetPasswordForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="密码由(a-zA-Z0-9)组成,6-30个字符" class="layui-input"
                       lay-verType="tips" lay-verify="required|password" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="resetPasswordSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/libs/layui/layui.js"></script>
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/js/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;

        /* 表单验证 */
        form.verify({
            account: function (value, item) {
                if (!new RegExp("^[a-zA-Z0-9_\.@]{6,30}$").test(value)) {
                    return '账号由(a-zA-Z0-9._@组合),6-30个字符';
                }
            },
            nickname: function (value, item) {
                if (value.length > 30) {
                    return '昵称最长30个字符';
                }
            },
            password: function (value, item) {
                if (!new RegExp("^[a-zA-Z0-9]{6,30}$").test(value)) {
                    return '密码由(a-zA-Z0-9组合),6-30个字符';
                }
            }
        });

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#test',
            url: '{{:url("Admin/index")}}',
            page: {
                limit: 20
            },
            method: 'post',
            contentType: 'application/json',
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '</p>'].join(''),
            cols: [[
                {field: 'id', title: 'ID', sort: true},
                {field: 'account', title: '账号'},
                {field: 'nickname', title: '昵称'},
                {field: 'role_name_txt', title: '角色名'},
                {field: 'created_at', title: '创建时间'},
                {field: 'updated_at', title: '更新时间'},
                {field: 'status', title: '状态', templet: '#statusDemo', width: 100},
                {title: '操作', toolbar: '#barDemo', align: 'center', width: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(search)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(test)', function (obj) {
            if (obj.event === 'edit') {//修改
                showSaveModel(obj.data);
            } else if (obj.event === 'del') {//删除
                doDel(obj);
            } else if (obj.event === 'resetPassword') {//重置密码
                showResetPasswordModel(obj.data);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(test)', function (obj) {
            if (obj.event === 'add') { // 添加
                showSaveModel();
            }
        });

        /* 显示表单弹窗 */
        function showSaveModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '用户',
                maxmin: true,
                resize: true,
                area: ['500px', '300px'],
                content: $('#adminSaveDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    var url = mData ? "{{:url('Admin/edit')}}" : "{{:url('Admin/add')}}";
                    if (mData) {
                        form.val('adminSaveForm', {
                            id: mData.id,
                            account: mData.account,
                            role_id: mData.role_id,
                            nickname: mData.nickname
                        });
                        $('#adminSaveDialog #account').attr("disabled", "disabled").addClass('layui-disabled');
                        $('#adminSaveDialog #password').removeAttr("lay-verify");
                    } else {
                        form.val('adminSaveForm', mData);
                    }

                    //表单提交事件
                    form.on('submit(adminSaveSubmit)', function (data) {
                        var loading = layer.load(3);

                        $.ajax({
                            url: url,
                            type: 'post',
                            data: data.field,
                            success: function (res) {
                                if (res.code != 0) {
                                    layer.msg(res.msg, {icon: 5,time:1000});
                                } else {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1,time:1500});
                                    insTb.reload();
                                }
                                layer.close(loading);
                            }, error: function () {
                                layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                                layer.close(loading);
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loading = layer.load(3);
                $.ajax({
                    url: "{{:url('Admin/del')}}",
                    type: 'post',
                    data: {id: obj.data.id},
                    success: function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 5,time:1000});
                        } else {
                            layer.msg(res.msg, {icon: 1,time:1500});
                            insTb.reload();
                        }
                        layer.close(loading);
                    }, error: function () {
                        layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                        layer.close(loading);
                    }
                });
                return false;
            });
        }

        /* 修改用户状态 */
        form.on('switch(editStatus)', function (obj) {
            var loading = layer.load(3);
            $.ajax({
                url: "{{:url('Admin/editStatus')}}",
                type: 'post',
                data: {
                    id: obj.elem.value,
                    status: obj.elem.checked ? 1 : 0
                },
                success: function (res) {
                    if (res.code != 0) {
                        layer.msg(res.msg);
                    }
                    layer.close(loading);
                }, error: function () {
                    layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                    layer.close(loading);
                }
            });
            return false;
        });

        /* 显示重置密码弹窗 */
        function showResetPasswordModel(mData) {
            admin.open({
                type: 1,
                title: '重置密码',
                maxmin: true,
                resize: true,
                area: ['600px', '200px'],
                content: $('#resetPasswordDialog').html(),
                success: function (layero, dIndex) {
                    form.val('resetPasswordForm', {
                        id: mData.id
                    });

                    // 表单提交事件
                    form.on('submit(resetPasswordSubmit)', function (data) {
                        var loadIndex = layer.load(3);

                        $.ajax({
                            url: "{{:url('Admin/resetPassword')}}",
                            type: 'post',
                            data: data.field,
                            success: function (res) {
                                layer.close(loadIndex);
                                if (res.code != 0) {
                                    layer.msg(res.msg, {icon: 5,time:1000});
                                } else {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1,time:1500});
                                    insTb.reload();
                                }
                            }, error: function () {
                                layer.close(loadIndex);
                                layer.close(dIndex);
                                layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                            }
                        });
                        return false;
                    });
                }
            });
        }
    });
</script>
</body>
</html>
